package com.esri.core.map;

import com.esri.core.internal.io.handler.ResponseListener;
import com.esri.core.internal.io.handler.a;
import com.esri.core.internal.io.handler.n;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.tencent.smtt.sdk.TbsVideoCacheTask;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class ServerFile {
    protected long _downloadSize;
    private String a;
    private String b;
    private ServerFolder c;
    private UserCredentials d;
    private String e;
    private ProgressListener f;

    /* loaded from: classes.dex */
    public interface ProgressListener {
        void reportProgress(String str, long j, long j2);
    }

    public ServerFile(ServerFolder serverFolder) {
        this.c = serverFolder;
    }

    public ServerFile(String str) throws MalformedURLException {
        this.a = new File(new URL(str).getPath()).getName();
        this.b = str;
    }

    public static ServerFile fromJson(JsonParser jsonParser, ServerFolder serverFolder, UserCredentials userCredentials) throws JsonParseException, IOException {
        if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
            return null;
        }
        ServerFile serverFile = new ServerFile(serverFolder);
        serverFile.setCredentials(userCredentials);
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String currentName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if (currentName.equals("name")) {
                serverFile.a = jsonParser.getText();
            } else if (currentName.equals(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL)) {
                serverFile.b = jsonParser.getText();
            } else {
                jsonParser.skipChildren();
            }
        }
        return serverFile;
    }

    public String getName() {
        return this.a;
    }

    public ServerFolder getParent() {
        return this.c;
    }

    public String getUrl() {
        return this.b;
    }

    public String getPath() {
        File file;
        if (this.e != null) {
            file = new File(this.e, this.a);
        } else if (this.c != null) {
            file = new File(this.c.getPath(), this.a);
        } else {
            file = new File(this.a);
        }
        return file.getPath();
    }

    public void setCredentials(UserCredentials userCredentials) {
        this.d = userCredentials;
    }

    public void setProgressListener(ProgressListener progressListener) {
        this.f = progressListener;
    }

    public String toString() {
        return this.a + "\r";
    }

    public File download(File file) throws EsriSecurityException, Exception {
        return download(file, false);
    }

    public File download(File file, boolean z) throws EsriSecurityException, Exception {
        File file2;
        InputStream inputStream;
        RandomAccessFile randomAccessFile;
        if (!z) {
            this.e = file.getPath();
            File file3 = new File(file, this.a);
            file2 = new File(file, this.a + ".part");
            file = file3;
        } else {
            this.e = file.getParent();
            file2 = new File(this.e, file.getName() + ".part");
        }
        if (!file.exists()) {
            long j = 0;
            if (file2.exists()) {
                j = file2.length();
            } else {
                File parentFile = file2.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                file2.createNewFile();
            }
            RandomAccessFile randomAccessFile2 = null;
            try {
                String str = this.b;
                inputStream = a.a(str, null, j, n.a(str, this.d), new ResponseListener() { // from class: com.esri.core.map.ServerFile.1
                    @Override // com.esri.core.internal.io.handler.ResponseListener
                    public boolean onResponseInterception(HttpResponse httpResponse) {
                        HttpEntity entity;
                        if (httpResponse == null || (entity = httpResponse.getEntity()) == null) {
                            return false;
                        }
                        ServerFile.this._downloadSize = entity.getContentLength();
                        return false;
                    }
                });
                try {
                    randomAccessFile = new RandomAccessFile(file2.getAbsoluteFile(), "rw");
                } catch (Throwable th) {
                    th = th;
                }
            } catch (Throwable th2) {
                th = th2;
                inputStream = null;
            }
            try {
                this._downloadSize += j;
                randomAccessFile.seek(j);
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = inputStream.read(bArr);
                    if (read == -1 || Thread.currentThread().isInterrupted()) {
                        break;
                    }
                    randomAccessFile.write(bArr, 0, read);
                    j += read;
                    a(j);
                }
                boolean z2 = !Thread.currentThread().isInterrupted();
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
                if (z2) {
                    file2.renameTo(file);
                }
            } catch (Throwable th3) {
                th = th3;
                randomAccessFile2 = randomAccessFile;
                if (randomAccessFile2 != null) {
                    try {
                        randomAccessFile2.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e4) {
                        e4.printStackTrace();
                    }
                }
                throw th;
            }
        }
        return file;
    }

    private void a(long j) {
        ProgressListener progressListener = this.f;
        if (progressListener != null) {
            progressListener.reportProgress(getName(), j, this._downloadSize);
        }
    }
}
